import java.util.Stack;

public class kuohaopipei {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i=0;i<s.length();i++) {
            if(s.charAt(i)=='('||s.charAt(i)=='{'||s.charAt(i)=='[') {
                stack.push(s.charAt(i));
            } else {
                if(stack.empty()) {
                    return false;
                }
                char tem = stack.peek();
                if((s.charAt(i)=='}'&&tem=='{') || (s.charAt(i)==']'&&tem=='[') || (s.charAt(i)==')'&&tem=='(')) {
                    stack.pop();
                } else {
                    return false;
                }
            }
        }
        if(!stack.empty()) {
            return false;
        }
        return true;
    }
}
